package com.hd.assistant.web.common.base;


import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.fastjson.JSONObject;
import com.hd.assistant.biz.manager.AssistantManager;
import com.hd.assistant.biz.manager.exception.ManagerException;
import com.hd.assistant.biz.service.PaiPaiIsvService;
import com.hd.assistant.biz.service.exception.ServiceException;
import com.hd.assistant.biz.util.PaipaiUtil;
import com.hd.assistant.dal.dataobject.User;



public abstract class BasePaipaiModule {
	public static String APP_TYPE= "appType";

	
	@Autowired
	protected AssistantManager assistantManager;
	
	@Autowired
	protected PaiPaiIsvService paiPaiIsvService;
	
	public abstract String getCurrentUserApp();

	public abstract String getCurrentAppId();
	
	protected User checkLogin(HttpSession session){
		Object o = session.getAttribute(PaipaiUtil.cookieMap.get(getCurrentAppId()));
		if( o != null){
			return (User)o;
		}else{
			return null;
		}
	}

	protected void supplyUserInfo(User u,Context context) throws ServiceException{
		JSONObject object = paiPaiIsvService.loadUserInfo(u);
		u.setUserNick(object.getString("nickName"));
		context.put("userInfo", object);
	
}

	
	protected User getUserInfo(Long uid) throws ManagerException {
		return assistantManager.getUserByUserIdAndType(uid, getCurrentUserApp());
	}
	
	protected void storeUserInfo(User u) throws ManagerException {
			assistantManager.insertUser(u);
		
	}
	
	protected void updateUserInfo(User u) throws ManagerException {
			assistantManager.updateUser(u);
	}
	
	protected User checkPermision(HttpSession session,Context context,Navigator nav) {	
       	User user = checkLogin(session);
        	if(user == null){
        		nav.redirectToLocation(PaipaiUtil.getRedirectLoginUrl(getCurrentAppId()));
        	}else{
        		context.put("user", user);
        		return user;
        	}
    	return null;
	}
}
